<?php
	// 加载公共文件
	if($_POST){
		include_once('../../public/config/sql.php');
		include_once('../../public/config/config.php');

		// 实例化对象
		$obj = new sql($info);

		$data = array(); // 初始化数组

		try{
			$rank = isset($_POST['rank']) ? explode(",", $_POST['rank']) : '';
			$menu = isset($_POST['menu']) ? explode(",", $_POST['menu']) : '';
			$id = isset($_POST['id']) ? explode(",", $_POST['id']) : '';
			$state = isset($_POST['state']) ? explode(",", $_POST['state']) : '';
			$link = isset($_POST['link']) ? explode(",", $_POST['link']) : '';
			$classify = isset($_POST['classify']) ? explode(",", $_POST['classify']) : '';

			// 修改或新增记录
			foreach($id as $key => $val){

				if($val === ''){

					// 构建SQL语句
					$sql = "insert into menu(cid,username,link,rank,state) values('$classify[$key]','$menu[$key]','$link[$key]','$rank[$key]','$state[$key]')";
					if($obj->db_insert($sql)){
						$msg = '1';
					}else{
						$msg = '0';
					}
				}else{

					$val = "cid='{$classify[$key]}',username='{$menu[$key]}',link='{$link[$key]}',rank='{$rank[$key]}',state='{$state[$key]}'";
					if($obj->db_update('menu', $val, $id[$key])){
						$msg = '1';
					}else{
						$msg = '0';
					}
				}
			}

			$data = array(
				'code' => '1',
				'msg' => $msg,
				'cid' => $classify
			);
		}catch(PDOException $e){

			$data = array(
				'code' => '0',
				'msg' => $e->getMessage()
			);
		}

		// 转换JSON类型数据
		header('Content-Type', 'application/json');
		echo json_encode($data);
	}
?>